﻿namespace Liger.Web.UI
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Liger.Web.Common;
    using Liger.Web.Common.Extensions;
    using System.ComponentModel;
    using Liger.Web.Javascript;
    using Liger.Web.Common.JSON;

    public class DateEditor : UIComponentBase
    {

        public DateEditor()
            : this(new StringBuilder())
        {

        }
        public DateEditor(StringBuilder writer)
            : base(writer)
        {
            this.Properties = new Dictionary<string, object>();
        }
		 
		public string Format
        {
            get;
            set;
        }
		public bool? ShowTime
        {
            get;
            set;
        }
		public bool? Absolute
        {
            get;
            set;
        }

        /// <summary>
        /// 其他可扩展的属性
        /// </summary>
        public Dictionary<string, object> Properties
        {
            get;
            private set;
        }

        /// <summary>
        /// 返回ligerDateEditor的Manager
        /// </summary>
        public string Manager
        {
            get;
            set;
        }

        public override void WriteHTML()
        {
            if (!ID.HasValue())
                ID = "DateEditor" + System.Guid.NewGuid().ToString();

            string TagName = string.IsNullOrEmpty(this.TagName) ? "div" : this.TagName;

            Writer.Append("<{1} id='{0}'></{1}>".FormatWith(ID, TagName));

            base.WriteScript();
        }
        public override void WriteScript()
        {
            JSWriter.WriteJQueryOnLoad();
            if (this.Manager.HasValue())
            {
                JSWriter.Append("window.{0} = ".FormatWith(this.Manager));
            }
            JSWriter.WriteJQueryPlugin(ID, "ligerDateEditor");
            JSWriter.Append(GetDateEditorParmJSON());
            JSWriter.WriteEndJQueryPlugin();
            JSWriter.WriteEndJQueryOnLoad();

            base.WriteScript();
        }

        public string GetDateEditorParmJSON()
        {
            IJavascriptObjectWriter JSWriter = new JavascriptObjectWriter();
            JSWriter.PropertyVariableCase = this.JSWriter.PropertyVariableCase;
            JSWriter.WriteOjbect(); 
			JSWriter.WriteProperty("Format", this.Format); 
			JSWriter.WriteProperty("ShowTime", this.ShowTime); 
			JSWriter.WriteProperty("Absolute", this.Absolute); 
            foreach (var prop in this.Properties.Keys)
            {
                var value = this.Properties[prop];
                JSWriter.WritePropertyObject(prop, value);
            } 
            foreach (var even in this.Events)
            {
                JSWriter.WriteProperty(even.Key, even.Value);
            } 
            JSWriter.WriteEndOjbect(); 
            return JSWriter.Output();
        }
    }
}